package com.jams.goodox.activity.mine.interaction;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hangzhou.jin.customview.JRecyclerView;
import com.hangzhou.jin.customview.RecycleViewDivider;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.adapter.MineInteractionChild3Adapter;
import com.jams.goodox.entity.MyActivityList;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Jin on 2016/5/14.
 * Description 我的互动-我的报名
 */
public class MineInteraction3Fragment extends BaseFragment {

	@Bind(R.id.jRecyclerView)
    JRecyclerView jRecyclerView;

	private MineInteractionChild3Adapter mAdapter;
	private List<MyActivityList.DataBean.ResultListBean> myList;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.j_recyclerview, container, false);
		ButterKnife.bind(this, view);

		jRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
		mAdapter = new MineInteractionChild3Adapter(new ArrayList<MyActivityList.DataBean.ResultListBean>());
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
            }
        });
		jRecyclerView.setAdapter(mAdapter);
		// 设置8dp的间隙
		//jRecyclerView.addItemDecorationDef();
		jRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, 1,getResources().getColor(R.color.base_background)));
		jRecyclerView.MarginTop(R.dimen.margin_size_small);
		jRecyclerView.setBackgroundResource(R.color.white_background);
		mAdapter.openLoadMore(PAGE_SIZE, true);

		// 自定义上拉View
		mAdapter.setLoadingView(getLoadMoreBootView(mContext));

		mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
			@Override
			public void onLoadMoreRequested() {
				loadData(false);
			}
		});

		loadData(true);

		jRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				// 刷新代码
				loadData(true);
			}
		});

		return view;
	}

	int curPage = 1;

	/**
	 * 请求网络数据
	 * @param isFirstPage 是否是第一页
	 */
	private void loadData(final boolean isFirstPage) {
		if (isFirstPage) curPage = 1;
		else curPage++;
		enqueue(createCallService(MineService.class).myApplyInteraction(curPage, PAGE_SIZE),new RetrofitCallbackListener<MyActivityList>() {
			@Override
			public void onSuccess(Call<MyActivityList> call, MyActivityList result) {
				jRecyclerView.setRefreshing(false);
				jRecyclerView.loadComplete();
				if (null != result.getData()) {
					myList = result.getData().getResultList();
					if (null != myList) {
						if (isFirstPage) {
							mAdapter.setNewData(myList);
							mAdapter.notifyDataChangedAfterLoadMore(myList.size() == PAGE_SIZE);
							if (myList.size() == 0) {
                                jRecyclerView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
								//return;
							}
						} else {
							mAdapter.notifyDataChangedAfterLoadMore(myList, myList.size() == PAGE_SIZE);
						}
					}
				}
			}

			@Override
			public void onError(String msg) {
				ToastUtil.show(msg);
				jRecyclerView.setRefreshing(false);
				if (!CollectionUtils.hasData(myList)) {
                    jRecyclerView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
				}else {
					jRecyclerView.loadComplete();
				}

			}
		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ButterKnife.unbind(this);
	}
}

